package com.thf.util;

import java.io.*;
import java.util.Arrays;

public class FileDataUtil {
    String inPath = "D:/test/titleData.txt";
    String outPath = "D:\\huarui03\\web\\WEB-INF\\note";
    String pathName = "titleData.txt" ;
    public static String createFile(String inPath ,String outPath , String outPathName){
        File file = new File(outPath);
        if(!file.exists()){
            file.mkdirs();
        }
        File text = new File(outPath,outPathName);
        try {
            text.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        copyFile(inPath,outPath+File.separator+outPathName);
        return outPath+File.separator+outPathName ;
    }
    //定义方法实现文件复制,一次复制一个字节数组,自动关闭流
    private static void copyFile(String srcFile, String destFile) {
        try (       //在try资源块中建立流通道
                    FileInputStream fis = new FileInputStream(srcFile);
                    FileOutputStream fos = new FileOutputStream(destFile);
        ){
            byte[] bytes = new byte[1024*8];

            int len = fis.read(bytes);
            while (len != -1){
                //把bytes数组中读到的前len个字节保存到目标文件中
                fos.write(bytes, 0, len);
                //继续从源文件中读取字节存储到字节数组中
                len = fis.read(bytes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    //获取文本文件数据
    public static String[] getFileData(String srcFile) throws IOException {
        FileInputStream fis = new FileInputStream(srcFile);
        InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
        BufferedReader br = new BufferedReader(isr);
        StringBuilder result;
        String line = br.readLine();
        result = new StringBuilder(line);
        while ( line != null ){
            result.append("\n");
            line = br.readLine();
            result.append(line);
        }
        br.close();
        String[] splits = result.toString().split("\n");
        splits = Arrays.copyOf(splits, splits.length - 1);
        return splits;
    }
}
